<?php
/**
 * 默认控制器
 * 
 * @version $Id: Site.php 679 2013-02-20 02:28:13Z linsir123 $
 */
class controllerSite extends AppController 
{
	/**
	 * 默认的控制器名称
	 */
	public $defaultAction = 'home';
	
	/**
	 * 首页
	 */
	public function actionHome()
	{
		if ($this->checkCache('home/index')) 
			return ;
			
		///
		View::assignSet(array(
			'taobaokes' => M::item()->getPHome(),
			/**
			 * 缓存已重新生成
			 */
			'shops' => App::O('recommendShop', 'home'),
			'dh' => App::O('classZhe', 'home'),
			'classes' => App::O('list', 'class'),
		));
		
		///
		AppView::addCss('themes/01/home.css');
	}
	
	/**
	 * 首页商铺列表
	 */
	public function actionHomeShops()
	{
		$i = intval($_GET['i']);
		if ($this->checkCache('home/_shops-div', $i)) 
			return ;
			
		///
		$shops = App::O('recommendShop', 'home');
		if ( ! array_key_exists($i, $shops)) 
			lpUrl::_r();
			
		///
		View::assignSet(array(
			'homeShopsIndex'=>$i,
			'homeShops'=>$shops[$i][1]
		));
	}
	
	/**
	 * 搜索
	 */
	public function actionSearch()
	{
		if (Request::isPost()) {
			$type = intval($_POST['t']);
			$keyword = trim($_POST['k']);
			
			///
			if (strpos($keyword, 'http')===0) {
				$tmp = parse_url($keyword);
				parse_str($tmp['query'], $tmp);
				if ( ! empty($tmp['id'])) {
					$params = array('iid'=>$tmp['id']);
					$url = Route::urlItem($params);
				}
			}
			
			///
			if (empty($url)) {
				switch ($type) {
					/// 商品
					case 0:
						$url = Route::urlClass(array(
							'keyword'=>$keyword, 
							'cid'=>intval($_POST['cid'])
						));
						break;
						
					/// 折扣
					case 1:
						$url = Route::urlZhe(array(
							'keyword'=>$keyword
						));
						break;
						
					/// 店铺
					case 2:
						$url = Route::urlShops(array(
							'keyword'=>$keyword, 
							'cid'=>intval($_POST['cid'])
						));
						break;
				}
			}
		} else 
			$url = lpUrl::__();
			
		///
		lpUrl::redirect($url);
	}
	
	/**
	 * 页面跳转
	 */
	public function actionRedirect()
	{
		$data = trim($_GET['data']);
		$action = trim($_GET['action']);
		if (empty($action) || empty($data)) 
			$this->goto301();
			
		///
		$url = null;
		switch ($action) {
			/**/
			case 'channel':
				$list = App::O('nav.channel');
				$url = isset($list[$data]) ? $list[$data][1] : null;
				break;
				
			/**/
			case 'tb':
				$data = base64_decode($data);
				$url = ! empty($data) ? $data : null;
				break;
		}
		
		///
		if ( ! empty($url)) {
			$this->log('Redirect', $url);
			
			///
			lpUrl::redirect($url);
		} else 
			$this->goto301();
	}
	
	/**
	 * 站点地图
	 */
	public function actionSitemap()
	{
		/**
		 * 缓存重建
		$classes = $this->mClass->getSub(0);
		View::assign('classes', $classes);
		 */
		
		View::display('sitemap/index');
	}
	
	/**
	 * 404
	 */
	public function actionMiss()
	{
		$this->goto301();
	}
}
